/**
 * Author: Daniel Michalski
 * Date: 05.03.13
 */

public class NewThread extends Thread {
    boolean suspendFlag;

    NewThread(String threadName, ThreadGroup tgOb) {
        super(tgOb, threadName);
        System.out.println("Nowy wątek: " + this);
        suspendFlag = false;
        start();
    }

    @Override
    public void run() {
        try {
            for (int i = 5; i > 0; i--) {
                System.out.println(getName() + ": " + i);
                Thread.sleep(1000);
                synchronized (this) {
                    while (suspendFlag)
                        wait();
                }
                
            }
        } catch (Exception ex) {
            System.out.println("Wyjątek w " + getName());
        }
        System.out.println(getName() + " zakończony.");
    }

    void mySuspend() {
        suspendFlag = true;
    }

    synchronized void myResume() {
        suspendFlag = false;
        notify();
    }
}
